// JavaScript Document
String.prototype.replaceAll = function (s1, s2) {
    return this.replace(new RegExp(s1, "gm"), s2);
}
$(function () {
    // $(".resultList").hide();
    // $("#resultSearch").hide();
    bindCheck();
    bindSearch();
});

function bindCheck() {
    $(".checkBoxs").children().bind("click", function (e) {
        var target = $(e.target);
        if (target.hasClass("checked")) {
            $("#checkBoxs1").children().eq(target.index()).removeClass("checked");
            $("#checkBoxs2").children().eq(target.index()).removeClass("checked");
            $(".logo").children().eq(target.index()).removeClass("checked");
        } else {
            $("#checkBoxs1").children().eq(target.index()).addClass("checked");
            $("#checkBoxs2").children().eq(target.index()).addClass("checked");
            $(".logo").children().eq(target.index()).addClass("checked");
        }
    });
}

function bindSearch() {
    $("#keyword1").focus();
    $("#searchIndex").bind("click", function () {
        $("#keyword1").focus();
    });
    $("#keyword1").bind("keypress", function (e) {
        if (e.which == 13) {
            search(1);
        }
    });
    $("#keyword2").bind("keypress", function (e) {
        if (e.which == 13) {
            search(2);
        }
    });
    $("#searchButton1").bind("click", function () {
        search(1)
    });
    $("#searchButton2").bind("click", function () {
        search(2)
    });
}

function search(id) {
    var checked = $("#checkBoxs2 .checked");
    var keywords = $("#keyword" + id).val();
    if (keywords.length < 1) {
        alert("请输入检索关键词");
        return;
    }
    if (checked.length < 1) {
        alert("请选择检索结果分类");
        return;
    }
    var checkedValue = "";
    for (var i = 0; i < checked.length; i++) {
        checkedValue += ("," + checked.eq(i).attr("value"));
    }

    if (id == 1)
        doSearch(checkedValue, keywords, function () {
            $("#searchIndex").fadeOut(300);
            $("#keyword2").val($("#keyword1").val());
            $("#resultSearch").css({"top": "20px", "opacity": "1"});
            $(".resultList").css({"top": "120px", "opacity": "1"});
        });
    else
        doSearch(checkedValue, keywords, function () {
        });
}

function doSearch(checkedValue, keywords, afterSuccess) {
    loadServerData("/index/search/queryList", {
        types: checkedValue.split(","),
        keywords: encodeURI(keywords)
    }, function (data) {
        afterSuccess();
        //加载返回来的列表数据
        $("#resultList ul").hide();
        var retDataMap = data.datas[0];
        var first,firstType;
        if (retDataMap["1"] != null) {
            $("#resultList ul").eq(0).show().find("li").remove();
            $(retDataMap["1"]).each(function (index, item) {
                var li = $("<li title='" + item.name + "'><span>" + item.typeName + "</span>" + item.name.substr(0, 8) +
                  //  (item.time == "0" ? "" : "<span class='right'>" + item.time + "</span>") +
                    (item.medtype == "1" ? "<span class='right'>省</span>" : "") +
                    (item.medtype == "2" ? "<span class='right'>市</span>" : "") +
                    "</li>")
                $("#resultList ul").eq(0).append(li);//控制左侧列表栏是否展示
                li.bind("click", function () {
                    $("#resultList ul .active").removeClass("active");
                   li.addClass("active");//改变样式
                   loadDetailData(item)//控制右侧数据的加载
                });
            })
            if (retDataMap["1"].length > 0) {
                first = retDataMap["1"][0];
                firstType=1;
                $("#resultList ul li").eq(0).addClass("active");//选中第一行数据
            }

        }
        if (retDataMap["2"] != null) {
            $("#resultList ul").eq(1).show().find("li").remove();
            $(retDataMap["2"]).each(function (index, item) {
                var li = $("<li title='" + item.name + "'><span>" + item.typeName + "</span>" + item.name.substr(0, 8) +
                   // (item.time == "0" ? "" : "<span class='right'>" + item.time + "</span>") +
                    (item.medtype == "1" ? "<span class='right'>省</span>" : "") +
                    (item.medtype == "2" ? "<span class='right'>市</span>" : "") +
                    "</li>")
                $("#resultList ul").eq(1).append(li);
                li.bind("click", function () {
                    $("#resultList ul .active").removeClass("active");
                    li.addClass("active");
                    loadDetailData2(item)
                });
            })
            if (retDataMap["2"].length > 0 && first == null) {
                first = retDataMap["2"][0];
                firstType=2;
                $("#resultList ul li").eq(0).addClass("active");
            }
        }
        if (retDataMap["3"] != null) {
            $("#resultList ul").eq(2).show().find("li").remove();
            $(retDataMap["3"]).each(function (index, item) {
                var li = $("<li title='" + item.cName + "'><span>" + item.iDeptId + "</span>" + item.cName.substr(0, 8) +
                    // (item.time == "0" ? "" : "<span class='right'>" + item.time + "</span>") +
                    (item.medtype == "1" ? "<span class='right'>省</span>" : "") +
                    (item.medtype == "2" ? "<span class='right'>市</span>" : "") +
                    "</li>")
                $("#resultList ul").eq(2).append(li);
                li.bind("click", function () {
                    $("#resultList ul .active").removeClass("active");
                    li.addClass("active");
                    loadDetailData3(item)
                });
            })
            if (retDataMap["3"].length > 0 && first == null) {
                first = retDataMap["3"][0];
                firstType=3;
                $("#resultList ul li").eq(0).addClass("active");
            }
        }
        if (retDataMap["4"] != null) {
            $("#resultList ul").eq(3).show().find("li").remove();
            $(retDataMap["4"]).each(function (index, item) {
                var li = $("<li title='" + item.cPatientName + "'><span>" + item.typeName + "</span>" +item.cPatientName.substr(0, 8) +
                    // (item.time == "0" ? "" : "<span class='right'>" + item.time + "</span>") +
                    (item.medtype == "1" ? "<span class='right'>省</span>" : "") +
                    (item.medtype == "2" ? "<span class='right'>市</span>" : "") +
                    "</li>")
                $("#resultList ul").eq(3).append(li);
                li.bind("click", function () {
                    $("#resultList ul .active").removeClass("active");
                    li.addClass("active");
                    loadDetailData4(item)
                });
            })
            if (retDataMap["4"].length > 0 && first == null) {
                first = retDataMap["4"][0];
                firstType=4;
                $("#resultList ul li").eq(0).addClass("active");
            }
        }
        //查询第一条列表内容
        if (first == null) {
            $("#detailWindow").html("<div class=\"title\">无匹配的数据</div><div class=\"subTitle\"></div>" +
                "            <div class=\"detailBody\">" +
                "            </div>");
        } else {
            switch(firstType){
                case  1:
                    loadDetailData(first);
                    break;
                case  2:
                    loadDetailData2(first);
                    break;
                case  3:
                    loadDetailData3(first);
                    break;
                case  4:
                    loadDetailData4(first);
                    break;
            }

        }
    }, "resultList");
}
function loadDetailData(param) {
    // $("#detailWindow").html("");
    if (param.medtype == "1")
        param.medtype = "省医保";
    else if (param.medtype == "2")
        param.medtype = "市医保";
    else if (param.medtype == "0")
        param.medtype = "省/市医保";
    else if(param.MedId==1){
        param.medtype="省医保"
    }else if(param.MedId==2){
        param.medtype="市医保"
    }
    switch (param.typeCode) {
        case "dbz":
            loadDetail_dbz({
                title: param.typeName,
                Drgsname: param.name,
                ybType: param.medtype,
                fixedmoney: param.money,
                dstartdate: param.key0,
                iDrgsId: param.id
            });
            break;
        case "mb":
            loadDetail_mxjb({
                title: param.typeName,
                Drgsname: param.name,
                ybType: param.medtype,
                fixedmoney: param.money,
                dstartdate: param.key0,
                ChronicRuleId: param.id
            },true);
            break;
        case "rj":
            loadDetail_rjss({
                title: param.typeName,
                Mictypename: param.name,
                ybType: param.medtype,
                opname: param.way,
                dstartdate: param.key0,
                Dayoperationid: param.id
            });
            break;
        case "yy":
            loadDetail_yy({
                title: param.typeName,
                cdgname: param.name,
                ybType: param.medtype,
                MictypeId: param.medtype,
                fixedmoney: param.key2,
                dstartdate: param.key0,
                DrugRuleId: param.id
            });
            break;
    }
}
function loadDetailData2(param) {
    // $("#detailWindow").html("");
    if (param.medtype == "1")
        param.medtype = "省医保";
    else if (param.medtype == "2")
        param.medtype = "市医保";
    else if (param.medtype == "0")
        param.medtype = "省/市医保";
    else if(param.MedId==1){
        param.medtype="省医保"
    }else if(param.MedId==2){
        param.medtype="市医保"
    }
    switch (param.typeCode) {
        case "dbz":
            loadDetail2_dbz({
                title: param.typeName,
                patname: param.name,
                ybType: param.medtype,
                no_treatlist: param.code,
                intime: param.time,
                fixedmoney: param.money,
                staffname: param.staffname,
                deptname: param.deptname,
                count:param.count
                //id: param.id
            });
            break;
        case "mb":
            loadDetail2_mxjb({
                title: param.typeName,
                patname: param.name,
                ybType: param.medtype,
                no_treatlist: param.code,
                intime: param.time,
                fixedmoney: param.money,
                staffname: param.staffname,
                deptname: param.deptname,
                csickname:param.csickname,
                inpcode:param.inpcode,
                count:param.count
            },true);
            break;
        case "pk":
            loadDetail2_pk({
                title: param.typeName,
                patname: param.name,
                ybType: param.medtype,
                no_treatlist: param.code,
                intime: param.time,
                fixedmoney: param.money,
                staffname: param.staffname,
                deptname: param.deptname,
                csickname:param.csickname,
                inpcode:param.inpcode,
                count:param.count
            });
            break;
        case "yy":
            loadDetail2_yy({
                title: param.typeName,
                no_treatlist: param.code,
                inpcode:param.inpcode,
                patname: param.name,
                staffname: param.staffname,
                csickname:param.csickname,
                deptname: param.deptname,
                fixedmoney: param.money,
                count:param.count
            });
            break;
        case "rj":
            loadDetail2_rjss({
                title: param.typeName,
                patname: param.name,
                ybType: param.medtype,
                no_treatlist: param.code,
                intime: param.time,
                fixedmoney: param.money,
                staffname: param.staffname,
                deptname: param.deptname,
                csickname:param.csickname,
                inpcode:param.inpcode,
                count:param.count
            });
            break;
    }
}
function loadDetailData3(param) {
    // $("#detailWindow").html("");
    if (param.medtype == "1")
        param.medtype = "省医保";
    else if (param.medtype == "2")
        param.medtype = "市医保";
    else if (param.medtype == "0")
        param.medtype = "省/市医保";
    else if(param.MedId==1){
        param.medtype="省医保"
    }else if(param.MedId==2){
        param.medtype="市医保"
    }

    loadDetail3_ys({
        title: '医生信息',
        No_Staff: param.No_Staff,
        cName: param.cName,
        iDeptId: param.iDeptId,
        TitleName: param.TitleName,
        cStaffCode: param.cStaffCode,
        ret_code: param.ret_code
    });
}
function loadDetailData4(param) {
    // $("#detailWindow").html("");
    if (param.medtype == "1")
        param.medtype = "省医保";
    else if (param.medtype == "2")
        param.medtype = "市医保";
    else if (param.medtype == "0")
        param.medtype = "省/市医保";
    else if(param.MedId==1){
        param.medtype="省医保"
    }else if(param.MedId==2){
        param.medtype="市医保"
    }
    switch (param.typeCode) {
        case "out":
             loadDetail4_out({
            title: param.typeName,
            iDiagnoseId: param.iDiagnoseId,
            cCaseCode: param.cCaseCode,
            cPatientName: param.cPatientName,
            iSex: param.iSex,
            cDiagResult: param.cDiagResult,
            iDeptID: param.iDeptID,
            dDiagnoseDate: param.dDiagnoseDate,
            iPatientTypeID: param.iPatientTypeID,
            iDoctorID: param.iDoctorID,
            iYearsOld:param.iYearsOld
        });
            break;
        case "in":
            loadDetail4_in({
                title: param.typeName,
                iDiagnoseId: param.iDiagnoseId,
                cCaseCode: param.cCaseCode,
                cPatientName: param.cPatientName,
                iSex: param.iSex,
                cDiagResult: param.cDiagResult,
                iDeptID: param.iDeptID,
                dDiagnoseDate: param.dDiagnoseDate,
                iPatientTypeID: param.iPatientTypeID,
                iDoctorID: param.iDoctorID,
                iYearsOld:param.iYearsOld
            },true);
            break;
    }
}


function loadServerData(url, data, render, loadingParent) {
    showLoading($("#" + loadingParent));
    $.ajax({
        url: url,
        data: {
            datas: JSON.stringify(data)
        },
        dataType: "json",
        success: function (ret) {
            hideLoading($("#" + loadingParent));
            render(ret);
        },
        error: function (ret) {
            alert("发生错误");
            console.debug(ret);
        }
    });
}

function showLoading(parent) {
    if (parent.find(".loading").length == 0)
        parent.append($("<div class='loading'></div>"));
}

function hideLoading(parent) {
    parent.find(".loading").fadeOut(300, function () {
        $(this).remove();
    });
}

function bindTab(buttonGroup, buttomClass, contentGroup) {
    $(buttonGroup).children().bind("click", function (e) {
        if ($(e.target).hasClass(buttomClass))
            index = $(e.target).index();
        else index = $(e.target).parents(buttomClass).index();
        $(contentGroup).find(".active").removeClass("active");
        $(buttonGroup).find(".active").removeClass("active");
        $(contentGroup).children().eq(index).addClass("active");
        $(buttonGroup).children().eq(index).addClass("active");
        e.stopPropagation();
    });
    $(buttonGroup).children().eq(0).addClass("active");
    $(contentGroup).children().eq(0).addClass("active");
}

// var keywords="青霉";
// $(".resultList").html($(".resultList").html().replaceAll(keywords,"<font class='keywords'>"+keywords+"</font>"));
